﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using OnlineManagement.DBUtility;
using System.Data;
using OnlineManagement.IDAL;
namespace OnlineManagement.SQLServerDAL
{
   public class TempRegist:ITempRegist
    {
        public TempRegist()
        { }
        #region  BasicMethod

       
        #endregion  BasicMethod

        #region  ExtensionMethod
        public bool CreateHoggery(OnlineManagement.Model.TempRegistUsers hoggery, out string returnValue,out int hoggeryId) 
        {
            hoggeryId = 0;
            returnValue = "";
            if(hoggery!=null
                &&!string.IsNullOrEmpty(hoggery.HoggeryName)
                &&!string.IsNullOrEmpty(hoggery.HoggeryManager))
            {
                string proceName = Common.PubProcedures.RegistHoggeryProce;
                SqlParameter[] parameters = {
					new SqlParameter("@hoggeryName", SqlDbType.NVarChar,50),
                    new SqlParameter("@managerName", SqlDbType.NVarChar,20),
					new SqlParameter("@userName", SqlDbType.NVarChar,50),
					new SqlParameter("@hoggeryId", SqlDbType.Int,4),
                    new SqlParameter("@rowsAffected",SqlDbType.TinyInt,1),
                    new SqlParameter("@msg",SqlDbType.NVarChar,200) };
                parameters[0].Value = hoggery.HoggeryName;
                parameters[1].Value = hoggery.HoggeryManager;
                parameters[2].Value = hoggery.LoginName;
                parameters[3].Direction = ParameterDirection.Output;
                parameters[4].Direction = ParameterDirection.Output;
                parameters[5].Direction = ParameterDirection.Output;
                try
                {
                    int rowsAffected;
                    DbHelperSQL.RunProcedure(proceName, parameters, out rowsAffected, out returnValue);
                    hoggeryId = rowsAffected;
                    return rowsAffected > 0 ? true : false;
                }
                catch (Exception ex)
                {
                    throw new Exception("数据库连接发生错误,请稍后再试。");
                }
            }
            returnValue = "注册信息不能为空";
            return false;
        }

        public int GetHoggeryIdForLogin(string userName) {
            if(string.IsNullOrEmpty(userName)){
                return 0; 
            }
            try
            {
                int hoggeryId = 0;
                string sql = "select HoggeryID from  Hoggery WHERE UserName=@userName ";
                SqlParameter[] paras = { new SqlParameter("@userName", SqlDbType.NVarChar, 20) };
                paras[0].Value = userName;
                SqlDataReader reader = DbHelperSQL.ExecuteReader(sql, paras);
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        int.TryParse(reader[0].ToString(), out hoggeryId);
                        return hoggeryId;
                    }
                }
                return hoggeryId;
            }catch(Exception ex){
                throw new Exception("数据库连接错误，请稍后再试。");
            }
        } 

        #endregion  ExtensionMethod
    }
}
